
var webpack = require('webpack');
var path = require('path')
var plugins = require('./plugins')
var modules = require('./module')
var vendors = require('./vendors')
var CopyWebpackPlugin = require('copy-webpack-plugin');
var CleanWebpackPlugin = require('clean-webpack-plugin');
//var StyleExtHtmlWebpackPlugin = require('style-ext-html-webpack-plugin');


var env = process.env.NODE_ENV
var params = process.env.NODE_PARAMS
// 打包的时候，生成文件的路径，区分生产环境和开发环境
var filePath = env === 'production' ? "../src/dist/":"../src/"

// 打包文件入口
var entry = {}
var _plugins = [
  plugins.buildCss('app.css'),
  new webpack.optimize.ModuleConcatenationPlugin(),
  new webpack.DefinePlugin({
    'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'development')
  }),
]


// 主项目
if(!params){
  entry = {
    'app'       : './app.js',
  }
  _plugins.push(
    plugins.getHtml(filePath+'index.html','../src/dll-index.html',{
      chunks: env==='production'?['app']:['app','vendors'],
      js:[ 
         "./lib/idangerous.swiper.min.js",
        // "./lib/iscroll.js", 
        "./lib/aes/sha.js",
        './lib/aes/aes.js',
        './lib/aes/pad-zeropadding.js'
      ]
    })
  )
}
if(env==='development'){
  _plugins.push(
    new webpack.DllReferencePlugin({
      context: path.join(__dirname,'..'),
      manifest: require( path.join(__dirname,'../vendors-manifest.json')),
    })
  )
}
if(env==='production'){
  // entry.vendors = vendors
  entry = {
    'app'       : './app.js',
    'vendors'   : vendors
  }
  _plugins.push(
    plugins.UglifyJsPlugin(),
    new webpack.optimize.CommonsChunkPlugin({ 
      name:     'vendors', 
      filename: 'vendors.js',
      
    }),
    // new StyleExtHtmlWebpackPlugin({
    //   minify: true
    // }),
    new CleanWebpackPlugin([
      'src/dist/assets',
      'src/dist/app.js',
      'src/dist/vendors.js',
      'src/dist/chunks',
    ],{
      root: process.cwd()
    }),
    new CopyWebpackPlugin([
      {
        from : path.join(__dirname,'../src/404.html'),
        to : path.join(__dirname,filePath+'/404.html')
      },
      {
        from : path.join(__dirname,'../src/lib'),
        to : path.join(__dirname,filePath+'/lib')
      }
    ])
  )
}
module.exports = {
  devtool : 'inline-source-map',
  context : path.resolve(__dirname,'../client'),
  entry: entry,
  output:{ 
    path:path.join(__dirname,filePath),
    publicPath:'/',
    chunkFilename: "chunks/chunks_[name].js",
    filename : '[name].js'
  },
  module:modules,
  plugins:_plugins

}







